1
|
|
|
/* eslint-disable camelcase */ |
2
|
|
|
import { Application } from "../../models/types"; |
3
|
|
|
import { ReviewStatusId } from "../../models/lookupConstants"; |
4
|
|
|
|
5
|
|
|
type Bucket = "priority" | "citizen" | "non-citizen" | "unqualified"; |
6
|
|
|
|
7
|
|
|
type Category = "primary" | "optional" | "screened-out"; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Return the bucket this application belongs to. Either: |
11
|
|
|
* priority |
12
|
|
|
* citizen |
13
|
|
|
* secondary |
14
|
|
|
* unqualified |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
export function applicationBucket(application: Application): Bucket { |
18
|
|
|
// TODO: Un-hardcode the application version ID. |
19
|
|
|
if (!application.meets_essential_criteria && application.version_id === 1) { |
20
|
|
|
if (application.citizenship_declaration.name !== "citizen") { |
21
|
|
|
return "non-citizen"; |
22
|
|
|
} |
23
|
|
|
return "unqualified"; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
if (application.applicant.user.is_priority) { |
27
|
|
|
return "priority"; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if (application.citizenship_declaration.name === "citizen") { |
31
|
|
|
return "citizen"; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return "non-citizen"; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Return the category this application belongs to. Either: |
39
|
|
|
* primary |
40
|
|
|
* optional |
41
|
|
|
* screened-out |
42
|
|
|
* @param {Application} application |
43
|
|
|
*/ |
44
|
|
|
export function applicationCategory(application: Application): Category { |
45
|
|
|
if (application?.application_review?.review_status?.name === "screened_out") { |
46
|
|
|
return "screened-out"; |
47
|
|
|
} |
48
|
|
|
if (application?.application_review?.review_status?.name === "still_in") { |
49
|
|
|
return "primary"; |
50
|
|
|
} |
51
|
|
|
const bucket = applicationBucket(application); |
52
|
|
|
|
53
|
|
|
switch (bucket) { |
54
|
|
|
case "priority": |
55
|
|
|
case "citizen": |
56
|
|
|
return "primary"; |
57
|
|
|
case "non-citizen": |
58
|
|
|
case "unqualified": |
59
|
|
|
default: |
60
|
|
|
return "optional"; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
function isVet(application: Application): boolean { |
65
|
|
|
return application.veteran_status.name !== "none"; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Compare function used for sorting applications |
70
|
|
|
*/ |
71
|
|
|
export function applicationCompare( |
72
|
|
|
first: Application, |
73
|
|
|
second: Application, |
74
|
|
|
): number { |
75
|
|
|
// Sort by status in the following order: |
76
|
|
|
// "still_in", "Not Reviewed", "still_thinking", "screened_out", |
77
|
|
|
const score = (application: Application): number => { |
78
|
|
|
switch ( |
79
|
|
|
application.application_review |
80
|
|
|
? application.application_review.review_status_id |
81
|
|
|
: null |
82
|
|
|
) { |
83
|
|
|
case ReviewStatusId.StillIn: |
84
|
|
|
return 1; |
85
|
|
|
case null: |
86
|
|
|
return 2; |
87
|
|
|
case ReviewStatusId.StillThinking: |
88
|
|
|
return 3; |
89
|
|
|
case ReviewStatusId.ScreenedOut: |
90
|
|
|
return 4; |
91
|
|
|
default: |
92
|
|
|
return 2; // Treat default same as "Not Reviewed" |
93
|
|
|
} |
94
|
|
|
}; |
95
|
|
|
|
96
|
|
|
// Add a preference for veterans within each status group |
97
|
|
|
const scoreVet = (application: Application): number => |
98
|
|
|
score(application) - (isVet(application) ? 0.1 : 0); |
99
|
|
|
const scoreDiff = scoreVet(first) - scoreVet(second); |
100
|
|
|
|
101
|
|
|
if (scoreDiff !== 0) { |
102
|
|
|
return scoreDiff; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Otherwise, sort alphabetically by name; |
106
|
|
|
return first.applicant.user.last_name.localeCompare( |
107
|
|
|
second.applicant.user.last_name, |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Compare function used for sorting applications, which prioritizes veterans over all others. |
113
|
|
|
*/ |
114
|
|
|
export function applicationComparePrioritizeVeterans( |
115
|
|
|
first: Application, |
116
|
|
|
second: Application, |
117
|
|
|
): number { |
118
|
|
|
// Veterans come before others |
119
|
|
|
if (isVet(first) && !isVet(second)) { |
120
|
|
|
return -1; |
121
|
|
|
} |
122
|
|
|
if (!isVet(first) && isVet(second)) { |
123
|
|
|
return 1; |
124
|
|
|
} |
125
|
|
|
return applicationCompare(first, second); |
126
|
|
|
} |
127
|
|
|
|